package com.link.base.base.dailystatement.controller;


import com.link.core.cllog.LCLogger;
import com.link.base.base.dailystatement.model.DailyDetail;
import com.link.base.base.dailystatement.service.DailyDetailService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  安小波
 * @date:   2018-08-01
 * @version 1.0
 */

@Controller
@RequestMapping("/link/dailyDetail")
public class DailyDetailController extends BasicController<DailyDetail> {

    @Resource
    private DailyDetailService dailyDetailService;

    @Override
    public BasicService<DailyDetail> getBasicService() throws Exception {
        return dailyDetailService;
    }

    /**
     * 查询日结单的详情信息
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryDailyDetails", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryDailyDetails(@RequestBody DailyDetail entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            //将查询的日结时间设为当前时间
            if (entity.getQueryDate() == null || "".equals(entity.getQueryDate())) {
                entity.setQueryDate(DateUtil.dateToStr(new Date(), "yyyy-MM-dd"));
            }
            DailyDetail detail = dailyDetailService.queryDailyDetails(entity);
            result.put("result", detail);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据id查询日结单详情信息
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryByHeadId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryDailyDetailsById(@JsonParam QueryParams qps,@JsonParam DailyDetail entity,
                                                     HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            DailyDetail detail = dailyDetailService.queryByHeadId(entity);
            result.put("result", detail);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 执行日结单详情页面的插入
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/dailyDetailsInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> dailyDetailsInsert(@RequestBody DailyDetail entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            //执行插入操作
            dailyDetailService.dailyDetailsInsert(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 将账单存储为PDF格式
     * @author guoshan
     * @date 2019/03/19
     **/
    @RequestMapping(value = "/printDailyBillPdf", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> printDailyBillPdf(DailyDetail dailyDetail, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (null == dailyDetail.getHeadId()) {
                throw  new ServiceException("PUBLIC-018");
            }
            //执行插入操作
            String  url = dailyDetailService.printDailyBillPdf(dailyDetail);
            result.put("pdfUrl",url);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", "保存为PDF失败");
        }
        return result;
    }


}
